package com.youmingxing.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.youmingxing.R;
import com.youmingxing.activity.selfInfo.PersonalInfoActivity;
import com.youmingxing.bean.selfinfo.SearchUser;
import com.youmingxing.network.AllStarApi;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.view.RefreshListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class SearchMoreUserActivity extends BaseActivity {
    RefreshListView searchListView;
    List<SearchUser> mSearchUsers = new ArrayList<>();
    int mark = 1;
    private String keyword;
    private SearchMoreUserAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseSetContentView(R.layout.activity_search_more_user);
        setTopbarName("更多选手");
        keyword = getIntent().getStringExtra("keyword");
        searchListView = (RefreshListView) findViewById(R.id.search_list);
        mark = 1;
        mSearchUsers.clear();
        AllStarApi.searchMoreUser(keyword, mark, initHandler);

        searchListView.setOnLoadMoreListener(new RefreshListView.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                System.out.println("什么时候调用");
                AllStarApi.searchMoreUser(keyword, mark, moreHandler);
            }
        });

        searchListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (id < 0) {
                    return;
                }
                int realPosition = (int) id;
                Intent intent = new Intent(SearchMoreUserActivity.this, PersonalInfoActivity.class);
                intent.putExtra("u_id", mSearchUsers.get(realPosition).getId());
                startActivity(intent);
            }
        });

    }

    BaseJsonHandler initHandler = new BaseJsonHandler(this) {
        @Override
        public void onSuccessWithData(JSONObject response) {
            JSONArray data = response.optJSONArray("data");

            System.out.println("data.length() =" + data.length());
            adapter = new SearchMoreUserAdapter();
            if (data.length() == 0 && mark == 1) {
                searchListView.noData("抱歉,没有您要的搜索结果");
            } else {
                searchListView.setFootVisible(false);
                for (int i = 0; i < data.length(); i++) {
                    JSONObject jsonObject = data.optJSONObject(i);
                    SearchUser searchUser = new SearchUser();
                    searchUser.setImg(jsonObject.optString("img"));
                    searchUser.setId(jsonObject.optInt("id"));
                    searchUser.setTitle(jsonObject.optString("title"));
                    searchUser.setType(jsonObject.optInt("type"));
                    searchUser.setTip(jsonObject.optString("tip"));
                    mSearchUsers.add(searchUser);
                }
                if (data.length() < 20) {
                    searchListView.setFootVisible(true);
                    searchListView.noMoreData();
                }
                adapter.notifyDataSetChanged();
                mark++;
            }
            searchListView.setAdapter(adapter);
        }
    };

    BaseJsonHandler moreHandler = new BaseJsonHandler(this) {
        @Override
        public void onSuccessWithData(JSONObject response) {
            searchListView.endLoadMore();
            mSearchUsers.clear();
            searchListView.setFootVisible(true);
            JSONArray data = response.optJSONArray("data");
            if (data.length() == 0) {
                if (mark == 1) {
                    searchListView.noData("抱歉,没有您要的搜索结果");
                } else {
                    searchListView.noMoreData();
                }
            } else {
                for (int i = 0; i < data.length(); i++) {
                    JSONObject jsonObject = data.optJSONObject(i);
                    SearchUser searchUser = new SearchUser();
                    searchUser.setImg(jsonObject.optString("img"));
                    searchUser.setId(jsonObject.optInt("id"));
                    searchUser.setTitle(jsonObject.optString("title"));
                    searchUser.setType(jsonObject.optInt("type"));
                    searchUser.setTip(jsonObject.optString("tip"));
                    mSearchUsers.add(searchUser);
                }
                adapter.notifyDataSetChanged();
                mark++;
            }
        }
    };

    private class ViewHolder {
        ImageView userImage;
        TextView userName;
        TextView userInfo;
    }

    private class SearchMoreUserAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mSearchUsers.size();
        }

        @Override
        public Object getItem(int position) {
            return mSearchUsers.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public android.view.View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(SearchMoreUserActivity.this, R.layout.list_item_search, null);
                holder = new ViewHolder();
                holder.userImage = (ImageView) convertView.findViewById(R.id.iv_image);
                holder.userName = (TextView) convertView.findViewById(R.id.tv_name);
                holder.userInfo = (TextView) convertView.findViewById(R.id.tv_info);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            SearchUser searchUser = mSearchUsers.get(position);
            Glide.with(SearchMoreUserActivity.this).load(searchUser.getImg()).placeholder(R
                    .drawable.default_16_9).animate(R.anim.abc_fade_in).into(holder.userImage);
            holder.userName.setText(searchUser.getTitle());
            holder.userInfo.setText(searchUser.getTip());
            return convertView;
        }

    }
}
